import 'dart:convert' show json;

import 'package:flutter/cupertino.dart';
import 'package:ribo_base_core/base_styles/base_chart_style.dart';

import '../ribo_base_core.dart';
import 'base_chart_axis.dart';
import 'base_chart_formatter.dart';
import 'base_model_asT.dart';

class BaseChartBarsLinesModel {
  ///触发方式为axis的tooltip，用于一次点击显示全系列信息，传值后其他tooltip方式不再显示
  BaseChartFormatter axisTooltipFormatter;

  /// https://echarts.apache.org/zh/option.html#tooltip.formatter
  BaseChartFormatter tooltipFormatter;

  /// https://echarts.apache.org/zh/option.html#series-pie.label.formatter
  BaseChartFormatter labelFormatter;

  /// 折线图的tooltipFormatter
  BaseChartFormatter lineTooltipFormatter;

  /// 折线图的labelFormatter
  BaseChartFormatter lineLabelFormatter;

  /// https://echarts.apache.org/zh/option.html#xAxis.axisLabel.formatter
  BaseChartFormatter xAxisLabelFormatter;

  /// https://echarts.apache.org/zh/option.html#xAxis.axisLabel.formatter
  BaseChartFormatter yAxisLabelFormatter;

  /// 折线图的xAxisLabelFormatter
  BaseChartFormatter lineXAxisLabelFormatter;

  /// 折线图的yAxisLabelFormatter
  BaseChartFormatter lineYAxisLabelFormatter;

  /// https://echarts.apache.org/en/tutorial.html#Rich%20Text
  Map<String, dynamic> xAxisLabelRich;

  /// https://echarts.apache.org/en/tutorial.html#Rich%20Text
  Map<String, dynamic> yAxisLabelRich;

  /// https://echarts.apache.org/en/tutorial.html#Rich%20Text
  Map<String, dynamic> lineXAxisLabelRich;

  /// https://echarts.apache.org/en/tutorial.html#Rich%20Text
  Map<String, dynamic> lineYAxisLabelRich;

  ///柱状图之间的间隔
  String barGap;

  /// 折线使用的axis index
  int lineAxisIndex;

  /// 柱状图使用的axis index
  int barAxisIndex;

  /// 折线使用的axis interval
  double lineAxisInterval;

  /// 柱状图使用的axis interval
  double barAxisInterval;

  /// 是否水平显示，默认垂直显示
  bool isHorizontal;

  /// 坐标轴显示设置
  BaseChartAxis axis;

  /// 图表样式
  BaseChartStyle style;

  /// 柱形图数据轴上的最小值
  double barAxisMin;

  /// 柱形图数据轴上的最大值
  double barAxisMax;

  /// 柱形图数据轴上的最小值
  double lineAxisMin;

  /// 柱形图数据轴上的最大值
  double lineAxisMax;

  /// 柱状图上文字颜色
  String barLabelColor;

  /// 横坐标是否为日期
  bool isDate;

  /// https://echarts.apache.org/zh/option.html#dataZoom
  Map<String, dynamic> dataZoom;

  /// 图表数据
  List<BaseChartBarsLinesSeries> series;

  BaseChartBarsLinesModel({
    this.axisTooltipFormatter,
    this.tooltipFormatter,
    this.labelFormatter,
    this.lineTooltipFormatter,
    this.lineLabelFormatter,
    this.barGap,
    this.axis,
    this.style,
    this.barAxisMin,
    this.barAxisMax,
    this.lineAxisMin,
    this.lineAxisMax,
    this.lineAxisIndex = 0,
    this.barAxisIndex = 0,
    this.lineAxisInterval,
    this.barAxisInterval,
    this.barLabelColor,
    this.isDate = true,
    this.isHorizontal = false,
    this.xAxisLabelFormatter,
    this.yAxisLabelFormatter,
    this.lineXAxisLabelFormatter,
    this.lineYAxisLabelFormatter,
    this.xAxisLabelRich,
    this.yAxisLabelRich,
    this.lineXAxisLabelRich,
    this.lineYAxisLabelRich,
    this.dataZoom,
    @required this.series,
  }) : assert(series != null);

  factory BaseChartBarsLinesModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<BaseChartBarsLinesSeries> series = jsonRes['series'] is List ? <BaseChartBarsLinesSeries>[] : null;
    if (series != null) {
      for (final dynamic item in jsonRes['series']) {
        if (item != null) {
          series.add(BaseChartBarsLinesSeries.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return BaseChartBarsLinesModel(
      axisTooltipFormatter: asT<BaseChartFormatter>(jsonRes['axisTooltipFormatter']),
      tooltipFormatter: asT<BaseChartFormatter>(jsonRes['tooltipFormatter']),
      labelFormatter: asT<BaseChartFormatter>(jsonRes['labelFormatter']),
      isHorizontal: asT<bool>(jsonRes['isHorizontal']),
      isDate: asT<bool>(jsonRes['isDate']),
      lineTooltipFormatter: asT<BaseChartFormatter>(jsonRes['lineTooltipFormatter']),
      lineLabelFormatter: asT<BaseChartFormatter>(jsonRes['lineLabelFormatter']),
      lineAxisIndex: asT<int>(jsonRes['lineAxisIndex']),
      barAxisIndex: asT<int>(jsonRes['barAxisIndex']),
      lineAxisInterval: asT<double>(jsonRes['lineAxisInterval']),
      barAxisInterval: asT<double>(jsonRes['barAxisInterval']),
      barLabelColor: asT<String>(jsonRes['barLabelColor']),
      barGap: asT<String>(jsonRes['barGap']),
      axis: asT<BaseChartAxis>(jsonRes['axis']),
      style: asT<BaseChartStyle>(jsonRes['style']),
      barAxisMin: asT<double>(jsonRes['barAxisMin']),
      barAxisMax: asT<double>(jsonRes['barAxisMax']),
      lineAxisMin: asT<double>(jsonRes['lineAxisMin']),
      lineAxisMax: asT<double>(jsonRes['lineAxisMax']),
      lineXAxisLabelFormatter: asT<BaseChartFormatter>(jsonRes['lineXAxisLabelFormatter']),
      lineYAxisLabelFormatter: asT<BaseChartFormatter>(jsonRes['lineYAxisLabelFormatter']),
      xAxisLabelFormatter: asT<BaseChartFormatter>(jsonRes['xAxisLabelFormatter']),
      yAxisLabelFormatter: asT<BaseChartFormatter>(jsonRes['yAxisLabelFormatter']),
      dataZoom: asT<Map<String, dynamic>>(jsonRes['dataZoom']),
      xAxisLabelRich: asT<Map<String, dynamic>>(jsonRes['xAxisLabelRich']),
      yAxisLabelRich: asT<Map<String, dynamic>>(jsonRes['yAxisLabelRich']),
      lineXAxisLabelRich: asT<Map<String, dynamic>>(jsonRes['lineXAxisLabelRich']),
      lineYAxisLabelRich: asT<Map<String, dynamic>>(jsonRes['lineYAxisLabelRich']),
      series: series,
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'axisTooltipFormatter': axisTooltipFormatter,
        'tooltipFormatter': tooltipFormatter,
        'labelFormatter': labelFormatter,
        'lineAxisIndex': lineAxisIndex,
        'lineAxisInterval': lineAxisInterval,
        'barAxisIndex': barAxisIndex,
        'barAxisInterval': barAxisInterval,
        'barLabelColor': barLabelColor,
        'isDate': isDate,
        'isHorizontal': isHorizontal,
        'lineTooltipFormatter': lineTooltipFormatter,
        'lineLabelFormatter': lineLabelFormatter,
        'lineXAxisLabelFormatter': lineXAxisLabelFormatter,
        'lineYAxisLabelFormatter': lineYAxisLabelFormatter,
        'xAxisLabelFormatter': xAxisLabelFormatter,
        'yAxisLabelFormatter': yAxisLabelFormatter,
        'barGap': barGap,
        'axis': axis,
        'barAxisMin': barAxisMin,
        'barAxisMax': barAxisMax,
        'lineAxisMin': lineAxisMin,
        'lineAxisMax': lineAxisMax,
        'style': style,
        'dataZoom': dataZoom,
        'xAxisLabelRich': xAxisLabelRich,
        'yAxisLabelRich': yAxisLabelRich,
        'lineXAxisLabelRich': lineXAxisLabelRich,
        'lineYAxisLabelRich': lineYAxisLabelRich,
        'series': series,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartBarsLinesSeries {
  BaseChartBarsLinesSeries({this.name, this.color, this.data, this.markLine, this.isLine = false, this.isGradient = false, this.gradientColor});

  factory BaseChartBarsLinesSeries.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : BaseChartBarsLinesSeries(
          name: asT<String>(jsonRes['name']),
          color: asT<String>(jsonRes['color']),
          data: asT<List<BaseChartBarsLinesData>>(jsonRes['data']),
          isLine: asT<bool>(jsonRes['isLine']),
          markLine: asT<Map<String, dynamic>>(jsonRes['markLine']),
        );

  /// 数据名称
  String name;

  /// https://echarts.apache.org/zh/option.html#series-line.markLine
  Map<String, dynamic> markLine;

  /// 颜色
  String color;

  /// 数据值
  List<BaseChartBarsLinesData> data;

  /// 是否是折线图，为否则为柱状图
  bool isLine;

  ///是渐变(目前仅用于柱状图，折线图不知道有没有效果)
  bool isGradient;

  ///渐变颜色 BaseChartFormatter.fromFunction("function(){return new echarts.graphic.LinearGradient(0, 0, 0, 1,[{offset: 0, color: '#036EB8'},{offset: 1, color: '#00A29A'}])}")
  BaseChartFormatter gradientColor;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'name': name,
        'data': data,
        'color': color,
        'isLine': isLine,
        'markLine': markLine,
        'isGradient': isGradient,
        'gradientColor': gradientColor,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartBarsLinesData {
  BaseChartBarsLinesData({String name, this.value, this.itemStyle, this.isDate = true}) : this.name = name.startsWith("20") && isDate ? name.replaceRange(0, 2, "") : name; //只显示最后的日期

  factory BaseChartBarsLinesData.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : BaseChartBarsLinesData(
          name: asT<String>(jsonRes['name']),
          value: asT<double>(jsonRes['value']),
          itemStyle: asT<Map<String, dynamic>>(jsonRes['itemStyle']),
        );

  /// 数据名称
  String name;

  /// 数据值
  double value;

  /// https://echarts.apache.org/zh/option.html#series-bar.itemStyle
  Map<String, dynamic> itemStyle;

  /// 是否为日期
  bool isDate;

  Map<String, dynamic> toJson() {
    var json = <String, dynamic>{'name': name, 'value': value, 'itemStyle': itemStyle};
    json.removeWhere((key, value) => value == null);
    return json;
  }

  @override
  String toString() {
    return json.encode(this);
  }
}
